package com.wang.users.service.impl;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wang.common.pojo.ApiResponse;
import com.wang.common.pojo.ApiResponseEnum;
import com.wang.common.pojo.JSONEnum;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.common.util.DateFormatUtil;
import com.wang.common.util.FileUtil;
import com.wang.common.util.PageInfoUtil;
import com.wang.users.dao.IACommentDao;
import com.wang.users.pojo.AComment;
import com.wang.users.pojo.ACommentEnum;
import com.wang.users.service.IGoodsDbService;

@Service("goodsDbService")
public class GoodsDbServiceImpl implements IGoodsDbService
{
	
	@Autowired
	private IACommentDao commentDao ;
	@Autowired
	private DateFormatUtil dateFormatUtil ;
	@Autowired
	private FileUtil fileUtil ;

	@Override
	public ApiResponse<Object> saveOneCommentService(AComment comment)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();

		comment.setStatus(ACommentEnum.STATUS_ENABLE.getStatus());

		comment.setCreateTime(new Date());
		comment.setUpdateTime(new Date());
		comment.setPubTime(new Date());
		int res = this.commentDao.save(comment);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), comment.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), comment);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneCommentService(AComment comment)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		comment.setUpdateTime(new Date());
		int res = this.commentDao.update(comment);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), comment.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), comment);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneCommentService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.commentDao.delete(condMap);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<AComment> findCondListCommentService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		ApiResponse<AComment> apiResponse = new ApiResponse<AComment>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<AComment> demoCateList = Collections.EMPTY_LIST;
		
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + "")) {
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}

		Map<String, List<AComment>> dataList = new HashMap<String, List<AComment>>();
		if(pageInfoUtil != null){
			Page<Object> page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			demoCateList = this.commentDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());
		}else {
			demoCateList = this.commentDao.findCondList(condMap);
		}
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoCateList);

		apiResponse.setPageInfoUtil(pageInfoUtil);
		apiResponse.setDataListJava(demoCateList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<AComment> findOneCommentService(Map<String, Object> condMap)
	{
		ApiResponse<AComment> apiResponse = new ApiResponse<AComment>();

		Map<String, AComment> data = new HashMap<String, AComment>();
		AComment comment = this.commentDao.findOne(condMap);
		data.put(ApiResponseEnum.NAME_ONE.getName(), comment);
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(comment);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}
	
}
